//
//  GridView.m
//  TabulaImaginarius2
//
//  Created by TA2 on 5/6/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "GridView.h"

@implementation GridView

@synthesize map;
@synthesize annotates;
@synthesize scale;
@synthesize tileSize;
@synthesize lineWidth;
@synthesize currentPoint;

+ (Class)layerClass {
	return [CATiledLayer class];
}

- (id)initWithMap:(Map *)newMap withTileSize:(CGSize)size withScale:(int)scale {
    if ((self = [super initWithFrame:CGRectMake(0, 0, newMap.size.width, newMap.size.height)])) {
        self.map = newMap;
		self.multipleTouchEnabled = YES;
        CATiledLayer *tiledLayer = (CATiledLayer *)[self layer];
		tiledLayer.tileSize = size;
		tiledLayer.levelsOfDetail = 3;
		tiledLayer.levelsOfDetailBias = 2;
		if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
			tiledLayer.levelsOfDetail = 2;
			tiledLayer.levelsOfDetailBias = 1;
		}
		self.scale = scale;
		tileSize = (CGSize){tiledLayer.tileSize.width/self.scale, tiledLayer.tileSize.height/self.scale};
    }
	self.backgroundColor = [UIColor clearColor];
	self.lineWidth = 6.0;
	[self makeGridImage];
    return self;
}

- (void)dealloc {
    [map release];
    map = nil;
    CGLayerRelease(gridImage);
    gridImage = nil;
    [super dealloc];
}

- (void)makeGridImage {
	UIGraphicsBeginImageContext((CGSize){120, 120});
	gridImage = CGLayerCreateWithContext(UIGraphicsGetCurrentContext(), (CGSize){120, 120}, NULL);
	UIGraphicsEndImageContext();
	UIGraphicsPushContext(CGLayerGetContext(gridImage));
	[[UIColor lightGrayColor] set];
	CGContextSetLineWidth(UIGraphicsGetCurrentContext(), lineWidth);
	CGContextBeginPath(UIGraphicsGetCurrentContext());
	CGContextMoveToPoint(UIGraphicsGetCurrentContext(), 10, 0);
	CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), 0, 0);
	CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), 0, 10 );
	CGContextMoveToPoint(UIGraphicsGetCurrentContext(), 110, 0);
	CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), 120, 0);
	CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), 120, 10);
	CGContextMoveToPoint(UIGraphicsGetCurrentContext(), 110, 120);
	CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), 120, 120);
	CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), 120, 110);
	CGContextMoveToPoint(UIGraphicsGetCurrentContext(), 0, 110);
	CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), 0, 120);
	CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), 10, 120);
	CGContextStrokePath(UIGraphicsGetCurrentContext());
	UIGraphicsPopContext();
}

- (void)drawRect:(CGRect)rect {
    // calculate the rows and columns of tiles that intersect the rect we have been asked to draw
    int firstCol = floorf(CGRectGetMinX(rect) / tileSize.width);
    int lastCol = floorf((CGRectGetMaxX(rect)-1) / tileSize.width);
    int firstRow = floorf(CGRectGetMinY(rect) / tileSize.height);
    int lastRow = floorf((CGRectGetMaxY(rect)-1) / tileSize.height);
	
    for (int row = firstRow; row <= lastRow; row++) {
        for (int col = firstCol; col <= lastCol; col++) {
			CGRect tileRect = CGRectMake(tileSize.width * col, tileSize.height * row,
                                         tileSize.width, tileSize.height);
			tileRect = CGRectIntersection(self.bounds, tileRect);
			CGContextDrawLayerInRect(UIGraphicsGetCurrentContext(), tileRect, gridImage);
        }
    }
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
	//
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
	//
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
	//
}

@end
